﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudentManage
{
    public partial class FrmAddClass : Form
    {
        public FrmAddClass()
        {
            InitializeComponent();
        }

        private void FrmAddClass_Load(object sender, EventArgs e)
        {
            InitGrade();//初始化年级列表
        }

        private void InitGrade()
        {
            string sqlGrade = "select GradeId,GradeName from GradeInfo";
            DataTable dtGradeCbo = SqlHelper.GetDataTable(sqlGrade);
            cboGradeName.DataSource = dtGradeCbo;
            cboGradeName.DisplayMember = "GradeName";
            cboGradeName.ValueMember = "GradeId";
        }
        /// <summary>
        /// 添加班级
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //获取输入的页面信息
            string className = txtClassName.Text.Trim();
            int gradeId = (int)cboGradeName.SelectedValue;
            string remark = txtRemark.Text.Trim();
            //判断是否为空
            if (string.IsNullOrEmpty(className)) 
            {
                MessageBox.Show("班名不能为空","添加班级提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            //判断班名是否已经存在
            {
                string sqlExists = "select * from classInfo where ClassName=@ClassName and GradeId=@GradeId";
                //设置参数
                MySqlParameter[] paras =
                {
                new MySqlParameter("@ClassName",className),
                new MySqlParameter("@GradeId",gradeId)
                };
                object obj = SqlHelper.ExecuteScalar(sqlExists, 1, paras);
                if (obj == null)
                {
                    //为空,说明不存在，进行添加操作
                    string addSql = "insert into classInfo(ClassName,GradeId,Remark)" +
                        "values(@ClassName,@GradeId,@Remark)";
                    MySqlParameter[] parasAdd =
                    {
                        new MySqlParameter("@ClassName", className),
                        new MySqlParameter("@GradeId", gradeId),
                        new MySqlParameter("@Remark", remark)
                    };

                    int rs = SqlHelper.ExecuteNonQuery(addSql,parasAdd);
                    if (rs > 0)
                    {
                        MessageBox.Show("班级添加成功", "添加班级提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("班级添加失败", "添加班级提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("班级已经存在", "添加班级提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }            
        }

        //关闭添加窗口
        private void btnCloseAdd_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
